package com.yiyw.herostory;


import com.yiyw.herostory.server.NettyServer;
import io.netty.channel.ChannelFuture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 *  Application 启动类
 *  @author 一夏
 */
@Slf4j
@SpringBootApplication
public class HeroStoryApplication implements CommandLineRunner {

    @Autowired
    NettyServer nettyServer;

    @Value("${netty.port:8888}")
    Integer nettyPort;

    public static void main(String[] args) {
        SpringApplication.run(HeroStoryApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        ChannelFuture channelFuture = nettyServer.nettyStart(nettyPort);
        // 增加虚拟机钩子函数
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            nettyServer.destroy();
        }));
        channelFuture.channel().closeFuture().channel();
    }
}
